<template>
  <view><button class="btn center" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">授权微信登录</button></view>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        code: '',
        avatar: '',
        nickname: ''
      }
    };
  },

  methods: {
    async decryptPhoneNumber(e) {
      let session_id = uni.getStorageSync('session_id');
      // 获取token
      let m = await this.$api.login({
        code: e.detail.code,
        encryptedData: e.detail.encryptedData,
        event: 'login',
        iv: e.detail.iv,
        session_id: session_id
      });
      uni.setStorageSync('token', m.data.token);
      if (m.code == 1) {
        this.$msg('登陆成功');
        uni.navigateBack();
      }
      //   uni.getUserProfile({
      //     desc: '用户信息',
      //     success: res => {
      //       console.log(res, '信息');
      //       this.formData.avatar = res.userInfo.avatarUrl;
      //       this.formData.nickname = res.userInfo.nickName;
      //       // 存本地
      //       uni.setStorageSync('img', res.userInfo.avatarUrl);
      //       uni.setStorageSync('username', res.userInfo.nickName);
      //       uni.login({
      //         provider: 'weixin',
      //         success: async res => {
      //           console.log(res, 'code'); //res.code
      //           this.formData.code = res.code;
      //           let n = await uni.$http.post('/User/register', this.formData);
      //           uni.setStorageSync('token', n.data.userinfo.token);
      //           uni.showToast({
      //             icon: 'none',
      //             title: '登陆成功'
      //           });
      //           setTimeout(() => {
      //             uni.switchTab({
      //               url: '/pages/my/my'
      //             });
      //           }, 1000);
      //         },
      //         fail: res => {
      //           console.log(res, '失败');
      //         }
      //       });
      //     }
      //   });
    }
  }
};
</script>

<style lang="scss">
.btn {
  width: 600rpx;
  height: 90rpx;
  border-radius: 60rpx;
  margin: 300rpx auto 0;
  background-color: #2b9939;
  border: 2rpx solid #2b9939;
  color: #fff;
}
</style>
